
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class ServeurSVG {

    private int numPort;
    private String nameFile = "test";
    private SVGWriter mySvgFile;

    public ServeurSVG(int _numPort) throws IOException {
        numPort = _numPort;
        mySvgFile = new SVGWriter(nameFile);
    }

    public void lancer() {
        try {
            ServerSocket serverSocket = new ServerSocket(numPort);
            boolean test = true;
            try {
                while (test) {
                    Socket socket = serverSocket.accept();

                    ObjectInputStream fluxDEntree = new ObjectInputStream(socket.getInputStream());

                    try {
                        MessageOrdre message = (MessageOrdre) fluxDEntree.readObject();
                        System.out.println("On recoit " + message);
                        traiteMessage(message);
                        if (message.getIntitule().equals("Fin")) {
                            System.out.println("Fermeture du serveur SVG");
                            test = false;
                        }
                    } catch (ClassNotFoundException e) {
                        System.err.println(e.getMessage());
                    }

                    socket.close();
                }
            } catch (IOException e2) {
                System.err.println(e2.getMessage());
            }
        } catch (IOException e1) {
            System.err.println(e1.getMessage());
        }
    }

    public void traiteMessage(MessageOrdre message) throws IOException {
        System.out.println("Traitement du message");
        System.out.println("Intitule : " + message.getIntitule() + " Process depart " + message.getIdProcessDepart() + " Process Fin " + message.getIdProcessFin() + " Date Debut " + message.getDateDepart() + " Date Fin " + message.getDateFin());


        if (message.getIntitule().equals("SC") || message.getIntitule().equals("sc")) {
            mySvgFile.dessineSC(message.getIdProcessDepart(), message.getDateDepart(), message.getDateFin());
        } else if (message.getIntitule().equals("REQ") || message.getIntitule().equals("req")) {
            mySvgFile.dessinerREQ(message.getIdProcessDepart(), message.getIdProcessFin(), message.getDateDepart(), message.getDateFin());
        } else if (message.getIntitule().equals("REP") || message.getIntitule().equals("rep")) {
            mySvgFile.dessinerREP(message.getIdProcessDepart(), message.getIdProcessFin(), message.getDateDepart(), message.getDateFin());
        } else if (message.getIntitule().equals("REAL") || message.getIntitule().equals("real")) {
            mySvgFile.dessinerREAL(message.getIdProcessDepart(), message.getIdProcessFin(), message.getDateDepart(), message.getDateFin());
        } else if (message.getIntitule().equals("ELEC") || message.getIntitule().equals("elec")) {
            mySvgFile.dessinerREP(message.getIdProcessDepart(), message.getIdProcessFin(), message.getDateDepart(), message.getDateFin());
        } else if (message.getIntitule().equals("Fin") || message.getIntitule().equals("fin")) {
            System.out.println("Je cloture le svg");
            mySvgFile.writeAllData();
        } else {
            System.out.println("Le message entré n'est pas compréhensible par le serveur");
        }
    }

    public static void main(String[] args) throws IOException {
        System.out.println("-- Creation du Serveur :");
        ServeurSVG serveur = new ServeurSVG(8080);
        serveur.lancer();
    }
}
